<style>
#uploadBtnHolder,#uploadBtnHolder2{display: block;width: 60px;height: 60px;}
</style>
<div class="page_title">
    <h2 class="fl">{$pageTitle}</h2>
    <a class="fr top_rt_btn iconfont iconfont-chexiao" href="{:U('admin/Gift/gift')}">返回列表</a>
</div>
<section>
    <ul class="ulColumn2">
        <li>
            <span class="item_name" style="width:120px;">礼物名称：</span>
            <input name="gift_name" type="text" class="textbox textbox_295" maxlength="4" placeholder="名称不得超过4个字"/>
            <!--<span class="errorTips"></span>-->
        </li>
        <li>
            <span class="item_name" style="width:120px;">所需金币数量：</span>
            <input name="gift_gold" type="text" class="textbox textbox_295" maxlength="5" value="0" onkeyup="this.value=this.value.replace(/\D/g,'')" onafterpaste="this.value=this.value.replace(/\D/g,'')"/>
            <div class="tishi"><i class="iconfont iconfont-tishi"></i>大于等于0的金币数量,0表示免费礼物(单位/个)</div>
        </li>
        <li>
            <span class="item_name" style="width:120px;">礼物唯一编号：</span>
            <input type="text" name="gift_code" class="textbox textbox_295" maxlength="50" placeholder="输入或生成礼物编号" onkeyup="this.value=this.value.replace(/\W/g,'')" onafterpaste="this.value=this.value.replace(/\W/g,'')" />
            <input type="button" class="link_btn" id="randGiftCode" value="随机生成" />
            <!--<span class="errorTips"></span>-->
        </li>
        <li>
            <span class="item_name" style="width:120px;">礼物展示图片：</span>
            <label class="uploadImg">
                <a id="uploadBtnHolder"></a>
                <span>上传图片</span>
                <input type="hidden" name="gift_img" value="" />
            </label>
            <div class="uploadImg-box clearfix" id="uploadImg_one" data-type="one">
            </div>
            <div class="tishi" style="display: block;margin:8px 120px;"><i class="iconfont iconfont-tishi"></i>建议上传96*96的png图片(要求背景透明,不超过500KB),其他比例请尽量上传等比例图片</div>
        </li>
        <li>
            <span class="item_name" style="width:120px;"></span>
            <input type="submit" class="link_btn" value="添加"/>
        </li>
    </ul>
</section>
<script>
$(function(){
    //上传初始化
    initSwf1 = admin.initUpload($("#uploadBtnHolder"),'gift',{ext:'*.png;',size:'500 KB'});
    //事件处理
    /** 上传进度 **/
    initSwf1.uploadProgress = function(file, bytesComplete, bytesTotal){
        var percent = Math.ceil((bytesComplete / file.size) * 100);
        if(percent >= 100){
            return true;
        }
        upload_progress_callcack($("#uploadImg_one"),percent,file.id)
    }
    /** 上传出错 **/
    initSwf1.uploadError = function(file, errorCode, message){
        upload_error($("#uploadImg_one"));
    }
    /** 上传完成 **/
    initSwf1.uploadSuccess = function(file, data, responseReceived){
        upload_success_callcack($("#uploadImg_one"),data);
    }
    
    //点击删除事件
    $("#uploadImg_one .uploadImg-delete").die().live('click',function(){
        $(this).parent().remove();
        var attach_id = $(this).data('id');
        admin.log(attach_id);
        var send_data = {
            url:U('admin/Gift/rm_gift_image'),
            async:true,
            data:{attach_id:attach_id}
        }
        send_ajax(send_data);
        $("input[name='gift_img']").val('');
        set_uploadImgBox($("#uploadImg_one"));
    })
    
    function upload_error(obj){
        $(".uploadImg-loading").html('上传失败,请重试');
        $(".uploadImg-loading").css({fontSize:'13px'});
    }
    //上传进度的回调方法
    function upload_progress_callcack(obj,num,fileID){
        if(!$('.uploadImg-loading')[0]){
            var item = '<label class="uploadImg-item" data-fileId="'+fileID+'" style="background:rgba(220, 219, 219, 0.68);">'+
                        '<span class="uploadImg-loading"></span>'+
                        '</label>';
            if(obj.data('type') != 'one'){
                obj.append(item);
            }else{
                obj.html(item);
            }
        }
        $(".uploadImg-loading").html(num+'%');
        set_uploadImgBox(obj);
    }
    //上传成功的回调方法
    function upload_success_callcack(obj,data){
        var data = eval('('+data+')');
        if(data.code != '00000'){
            upload_error(obj);
            msg_error(data.message);
            return false;
        }
        //上次上传的
        var last_attach_id = $("input[name='gift_img']").val();
        if(last_attach_id){
            var send_data = {
                url:U('admin/Gift/rm_gift_image'),
                async:true,
                data:{attach_id:last_attach_id}
            }
            send_ajax(send_data);
        }
        var urls = data.data[0].urls;
        var item = '<label class="uploadImg-item" style="background:url('+urls[0]+') no-repeat center;background-size: 100%;">'+
                    '<span class="uploadImg-delete" data-id='+data.data[0].attach_id+'></span>'+
                    '</label>';
        if(obj.data('type') != 'one'){
            obj.append(item);
        }else{
            obj.html(item);
        }
        var attach_id = data.data[0].attach_id;
        
        $("input[name='gift_img']").val(attach_id);
        $(".uploadImg-loading").parent().remove();
        set_uploadImgBox(obj);
    }
    //计算并设置上传文件列表框的位置
    function set_uploadImgBox(obj){
        var n = obj.find(" .uploadImg-item").length;
        if(n == 0){
            obj.hide();
            return true;
        }else{
            obj.show(500);
            var del_w = parseInt(obj.find(".uploadImg-delete:eq(0)").width())/2;
        }
        var w = parseInt(15) * (n+parseInt(1)) + n * parseInt(obj.find(".uploadImg-item:eq(0)").width()) - del_w;
        obj.css('width',w+'px');
    }
    /** 
      * 获取随机生成的礼物代号 
      **/
    $('#randGiftCode').click(function(){
        var send_data = {
            url:U('admin/Gift/rand_gift_code'),
        }
        send_ajax(send_data,function(data){
            if(data.status == 1){
                $('input[name="gift_code"]').val(data.data.gift_code);
            }else{
                msg_error(data.message);
            }
        });
        return true;
    });
    /**
      * 提交
      */
    $("input[type='submit']").click(function(){
        var data = $.extend({},{
            gift_name:$("input[name='gift_name']").val(),
            gift_gold:parseInt($("input[name='gift_gold']").val()),
            gift_code:$("input[name='gift_code']").val(),
            gift_img:$("input[name='gift_img']").val(),
            
        });
        var send_data = {
            url: U("admin/Gift/do_add_gift"),
            data:data,
            type:'POST',
            dataType:'json',
        }
        send_ajax(send_data,function(res){
            if(res.status == 1){
                msg_success(res.data.info,'',1000);
                setTimeout(function(){
                    admin.getContentByUrl(res.data.jumpurl);
                },1000);
            }else{
                msg_error(res.message,'',1800);
            }
        });
        
    });
})
</script>